Skip to content

Conversation

@cycledriver
Copy link

Needed to figure out how to use an alias to refer to the same table with 2 different attributes. Thought I would share share my learning by adding to the tutorial.

@github-actions
Copy link
Contributor

📝 Docs preview for commit 9606fac at: https://2236fb7a.sqlmodel.pages.dev

@github-actions
Copy link
Contributor

📝 Docs preview for commit daf8e90 at: https://537fb381.sqlmodel.pages.dev

@github-actions
Copy link
Contributor

📝 Docs preview for commit 82e8655 at: https://4ab2c1a2.sqlmodel.pages.dev

@github-actions
Copy link
Contributor

📝 Docs preview for commit 046ef20 at: https://10831b49.sqlmodel.pages.dev

When one object has more than one relationship
to the same foreign object, you need to use
`aliased` to differentiate between the relationships.
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2024

📝 Docs preview for commit bd2d61a at: https://84f23520.sqlmodel.pages.dev

@alejsdev alejsdev added the docs Improvements or additions to documentation label Jul 5, 2024
@github-actions
Copy link
Contributor

Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cycledriver, thanks for working on this!
I really like this!

Are you ready to work a little more on this PR to polish it?

Comment on lines 32 to 46
```Python hl_lines="11 15"
# Code above omitted 👆

{!./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001.py[ln:13-26]!}

# Code below omitted 👇
```

/// details | 👀 Full file preview

```Python
{!./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001.py!}
```

///
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```Python hl_lines="11 15"
# Code above omitted 👆
{!./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001.py[ln:13-26]!}
# Code below omitted 👇
```
/// details | 👀 Full file preview
```Python
{!./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001.py!}
```
///
{* ./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001_py310.py ln[13:26] hl[11,15] *}

The format of includes has been changed and now it's much simpler

```

///
## Searching for Heros
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about adding a "Get hero's teams" section and show how easy it is to access Hero.winter_team and Hero.summer_team?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment was marked as duplicate.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant something like

hero = session.get(Hero, hero_id)
print(f"Hero: {hero.name}, Winter Team: {hero.winter_team.name} Summer Team: {hero.summer_team.name}")

)
summer_team_id: int | None = Field(default=None, foreign_key="team.id")
summer_team: Team | None = Relationship(
sa_relationship_kwargs={"primaryjoin": "Hero.summer_team_id == Team.id"}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more question - why did you choose to specify primaryjoin and not foreign_keys here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember. Our code was probably using "primaryjoin" for other things already. I'll give both a try.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like primaryjoin and foreign_keys work the same way in this case. I dug into the sqlalchemy docs a bit more and it looks like foreign_keys is the recommended way to to this: https://docs.sqlalchemy.org/en/20/orm/join_conditions.html#handling-multiple-join-paths.

@github-actions github-actions bot removed the waiting label Aug 27, 2025
@github-actions
Copy link
Contributor

@github-actions
Copy link
Contributor

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

@cycledriver
Copy link
Author

Did some more research and found two ways to do this. Added a bit more info about both ways and why you might choose one over the other.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

@github-actions
Copy link
Contributor

github-actions bot commented Sep 1, 2025

Comment on lines +72 to +78
```Python hl_lines="11 15"
# Code above omitted 👆

{!./docs_src/tutorial/relationship_attributes/multiple_relationships_same_model/tutorial001.py[ln:13-26]!}

# Code below omitted 👇
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use new code include format:

{* ./docs_src/tutorial/relationship_attributes/aliased_relationship/tutorial001_py310.py ln[13:26] hl[11,15] *}

```

///
## Searching for Heros

This comment was marked as duplicate.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 9, 2025

As this PR has been waiting for the original user for a while but seems to be inactive, it's now going to be closed. But if there's anyone interested, feel free to create a new PR.

@github-actions github-actions bot closed this Oct 9, 2025
@YuriiMotov
Copy link
Member

@cycledriver, do you have plans to continue working on this?
We can re-open this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants